<?php

echo "🧪 测试菜单响应结构修复\n";
echo "===========================\n\n";

echo "1. 测试菜单树接口响应结构...\n";

// 模拟一个简单的请求到菜单树接口
$ch = curl_init('http://127.0.0.1:8000/api/menus/tree');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Accept: application/json',
    'Content-Type: application/json'
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "   请求: GET /api/menus/tree\n";
echo "   状态码: $httpCode\n";

if ($httpCode === 200) {
    $data = json_decode($response, true);

    // 检查响应结构
    $isArray = is_array($data);
    $hasNestedSuccess = isset($data['success']);
    $hasNestedData = isset($data['data']) && is_array($data['data']);

    echo "   📋 响应结构分析:\n";
    echo "     - 响应类型: " . ($isArray ? "数组" : "对象") . "\n";
    echo "     - 是否有嵌套包装: " . ($hasNestedSuccess ? "是" : "否") . "\n";

    if ($isArray) {
        echo "     - 数组长度: " . count($data) . "\n";
        if (count($data) > 0) {
            echo "     - 第一个菜单项: " . json_encode($data[0], JSON_UNESCAPED_UNICODE) . "\n";
        }
        echo "     ✅ 响应结构正确！前端可以直接使用 response.data\n";
    } elseif ($hasNestedData) {
        echo "     - 嵌套数据长度: " . count($data['data']) . "\n";
        echo "     ❌ 仍有嵌套包装，前端需要使用 response.data.data\n";
    } else {
        echo "     ❌ 响应结构异常\n";
    }

    echo "\n   📝 完整响应预览:\n";
    echo "   " . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";

} else {
    echo "   ❌ 请求失败: $httpCode\n";
    echo "   响应: $response\n";
}

echo "\n2. 前端数据访问方式对比...\n";

if ($httpCode === 200) {
    $data = json_decode($response, true);

    if (is_array($data)) {
        echo "   ✅ 修复后: response.data (直接是菜单数组)\n";
        echo "   ❌ 修复前: response.data.data (需要两层访问)\n";
    } else {
        echo "   ❌ 修复失败，仍需要两层访问\n";
    }
}

echo "\n🎯 测试完成!\n";
